package springboot.Tools;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

public class time {

  static public SimpleDateFormat simpleYearAllDateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
  static public SimpleDateFormat monthDayString = new SimpleDateFormat("MM-dd");
  static public SimpleDateFormat monthString = new SimpleDateFormat("MM");
  static public SimpleDateFormat fullDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


  static public Date StringToFormatDate(String dateString) {
    if (Objects.equals(dateString, "null")) {
      return null;
    }
    if (dateString.length() < 13) {
      StringBuilder dateBuilder = new StringBuilder(dateString);
      for (int i = 0; i <= 14 - dateBuilder.length(); i++) {
        dateBuilder.append('0');
      }
      dateString = dateBuilder.toString();
    }
    return new Date(Long.parseLong(dateString));
  }


  static public String dateFormat(Date date, int index) {
    switch (index) {
      case 1:
        return fullDateFormat.format(date);
      case 2:
        return monthDayString.format(date);
      case 3:
        return monthString.format(date);
      default:
        return simpleYearAllDateFormat.format(date);
    }
  }

  static public String dateFormat(Date date) {
    return monthDayString.format(date);
  }

  static public Date StringToDate(String dateString) {
    Date date;
    if (dateString.length() < 13) {
      StringBuilder dateBuilder = new StringBuilder(dateString);
      for (int i = 0; i <= 14 - dateBuilder.length(); i++) {
        dateBuilder.append('0');
      }
      dateString = dateBuilder.toString();
    }
    date = new Date(Long.parseLong(dateString));
    return date;
  }
}
